 const App = getApp();
const wxParse = require("../../../wxParse/wxParse.js");
Page({

  /**
   * 页面的初始数据
   */ 
  data: {
    num:1,
    lpShow:false,
    checked:1,
    isPay:false,
    isRule:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        console.log(options)
        //去选择地址后带回address_id
        // let{address_id}=options
        this.setData({
          goods_id:wx.getStorageSync('goods_id'),
          num:wx.getStorageSync('goods_num'),
          goods_sku_id:wx.getStorageSync('goods_sku_id'),
          address_id:options.address_id||''
        })
        this.getData()
        if(options.address_id){
          this.getAddress(options.address_id)
        }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()
    if(this.data.address_id){
      this.getAddress(this.data.address_id)
    }
  },
  // 去选择地址
  selectAddress(){
      wx.navigateTo({
        url: '/pages/address/index?type=jfNow',
      })
  },
  //获取数据
  getData(){
    var _this=this
    App._post_form('order/IntegralBuyInfo',{
      goods_id:_this.data.goods_id,
      goods_num:_this.data.num,
      goods_sku_id:_this.data.goods_sku_id,
    },function(res){
        console.log(res)
        let orderInfo=res.data
        _this.setData({
          orderInfo,
          address:orderInfo.address_default,
          address_id:orderInfo.address_default?orderInfo.address_default.address_id:""
        })
    })
  },
  //获取地址详情
  getAddress(id){
    var that=this
    App._post_form('address/detail/address_id/'+id,{},function(res){
       console.log(res)
       let address=res.data.detail
       that.setData({
         address
       })
    })
  },
  checkboxChange(e){
    console.log(e)
    let val=e.detail.value
    if(val.length==0){
      this.setData({
        isPay:false
      })
    }else{
      this.setData({
        isPay:true
      })
    }
  },
  closeRule(){
    this.setData({
      isRule:false
    })
  },
  //隐私条款
  lookXy(){
    var that=this
    App._post_form('order/getAgreement',{},function(res){
       console.log(res)
        // 富文本转码
        if (res.data.length > 0) {
          wxParse.wxParse('content', 'html', res.data, that, 0);
        }
        that.setData({
          isRule:true
        })
    })
  },
  //微信支付
  wxpay(){
    let isPay=this.data.isPay
    if(!isPay){
      wx.showToast({
        title: '请勾选隐私政策',
        icon:'none'
      })
      return
    }
    if(!this.data.address_id){
      wx.showToast({
        title: '请选择收货地址',
        icon:'none'
      })
      return
    }
    var that=this
    console.log(this.data.address)
    App._post_form('order/integralNowBuy',{
      goods_id:that.data.goods_id,
      goods_num:that.data.num,
      goods_sku_id:that.data.goods_sku_id,
      address_id:that.data.address.address_id
    },(res)=>{
      console.log(res)
      let payment=res.data.payment
      if(res.code==1){
        if(payment){
          wx.requestPayment({
            timeStamp:payment.timeStamp,
            nonceStr: payment.nonceStr,
            package:payment.package,
            signType:payment.signType,
            paySign:payment.paySign,
            success (res) {
                wx.showToast({
                  title: '支付成功',
                })
                wx.navigateTo({
                  url: '../order/index?navActive=1',
                })
             },
            fail (res) {
              // wx.showToast({
              //   title: '取消支付',
              // })
             }
          })
        }else{
          wx.showToast({
            title: '支付成功',
          })
          wx.navigateTo({
            url: '../order/index?navActive=1',
          })
        }
      }else{
        wx.showToast({
          title: res.msg,
        })
      }
      
    })

    
  },
  // 展开礼品
  isLpShow(){
      this.setData({
        lpShow:!this.data.lpShow
      })
  },
  //减
  jian(){
    let num=this.data.num
    if(num<2){
      // App.showSuccess('数量不能小于1')
      wx.showToast({
        title: '数量不能小于1',
        icon:'none'
      })
      return
    }
    num--
    this.setData({
      num:num
    })
    this.getData()
  },
  //加
  add(){
     let num=this.data.num
     num++
     this.setData({
       num:num
     })
     this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})